/**
 * 
 * This class displays the preferences screen
 * This preference menu was build using the information from:
 * http://developer.android.com/resources/tutorials/views/hello-formstuff.html
 * 
 * Alert dialogs code based on example from example from http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/AlertDialogSamples.html
 *
 */

package com.tagit;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Preferences extends Activity {

	Button btDone;
	Button btFavoriteTags;
	Button btRejectedPics;
	Button btDownloaded;

	public static final String PREFS_NAME = "MyPrefsFile"; // Store Favorite tag
															// booleans

	// and strings here

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.preferences);

		btDone = (Button) findViewById(R.id.done);
		btFavoriteTags = (Button) findViewById(R.id.favoriteTags);
		btRejectedPics = (Button) findViewById(R.id.rejectedPics);
		btDownloaded = (Button) findViewById(R.id.downloadprefs);

		// Set listener for "Done" button
		btDone.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				finish();
			}
		});

		// Set listener for "Favorite Tags" button - send user to config menu
		btFavoriteTags.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				Intent intent = new Intent(Preferences.this, FavoriteTags.class);
				startActivity(intent);

			}
		});

		// Set listener for Reject photos button - send user to Rejected photos
		// folder
		btRejectedPics.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				Intent intent = new Intent(Preferences.this,
						PhotoGalleryRejected.class);
				startActivity(intent);

			}
		});

		// Display alert dialog for download preferences button when clicked -
		// mocked up
		btDownloaded.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {

				// Open shared preferences
				final SharedPreferences settings = btDownloaded.getContext()
						.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);

				// Pull out stored index for number of photos picked last time
				int defaultIndex = settings.getInt("numPhotos", 0);

				// Hard coded set of numbers of photos - could be more dynamic
				// if connected to Flickr API
				final CharSequence[] downloadoptions = { "50 photos",
						"100 photos", "150 photos", "200 photos", "250 photos" };

				// Build an alert dialog that asks the user how many photos he
				// wants to download from Flickr
				AlertDialog.Builder builder = new AlertDialog.Builder(v
						.getContext());
				builder.setTitle("How many photos would you like to download from Flickr?");
				builder.setSingleChoiceItems(downloadoptions, defaultIndex,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int whichButton) {

								// Reset the default index for the number of
								// photos to display
								SharedPreferences.Editor editor = settings
										.edit();
								editor.putInt("numPhotos", whichButton);
								editor.commit();
							}
						});

				builder.setPositiveButton("Continue",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								// Could do something with the Flickr API here
							}
						});

				AlertDialog alert = builder.create();
				alert.show();

			}
		});
	}
}